package com.hontech.kaltaapp.ui.parts

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.hontech.kaltaapp.R
import com.hontech.kaltaapp.device.event.DeviceStatus
import com.hontech.kaltaapp.device.partsCtrl
import com.hontech.kaltaapp.ui.dialog.showErr
import com.hontech.kaltaapp.ui.view.LabelSwitch
import com.hontech.kaltaapp.utils.register
import com.hontech.kaltaapp.utils.unregister
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class CtrlParts(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    private val swLed: LabelSwitch
    private val swRoomFan: LabelSwitch
    private val swDrainFan: LabelSwitch
    private val swDrainPump: LabelSwitch
    private val swV1: LabelSwitch
    private val swV2: LabelSwitch
    private val swV3: LabelSwitch
    private val swV4: LabelSwitch
    private val swV5: LabelSwitch
    private val waterHeat: LabelSwitch
    private val steamHeat: LabelSwitch
    private val drawPump: LabelSwitch

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_ctrl, this)
        swLed = view.findViewById(R.id.id_parts_ctrl_room_led)
        swRoomFan = view.findViewById(R.id.id_parts_ctrl_room_fan)
        swDrainFan = view.findViewById(R.id.id_parts_ctrl_drain_fan)
        swDrainPump = view.findViewById(R.id.id_parts_ctrl_drain_pump)
        swV1 = view.findViewById(R.id.id_parts_ctrl_v1)
        swV2 = view.findViewById(R.id.id_parts_ctrl_v2)
        swV3 = view.findViewById(R.id.id_parts_ctrl_v3)
        swV4 = view.findViewById(R.id.id_parts_ctrl_v4)
        swV5 = view.findViewById(R.id.id_parts_ctrl_v5)
        waterHeat = view.findViewById(R.id.id_parts_ctrl_water_heat)
        steamHeat = view.findViewById(R.id.id_parts_ctrl_steam_heat)
        drawPump = view.findViewById(R.id.id_parts_ctrl_draw_pump)

        swLed.onToggleFn = {
            ctrl(0, it)
        }
        swRoomFan.onToggleFn = {
            ctrl(1, it)
        }
        swDrainFan.onToggleFn = {
            ctrl(2, it)
        }
        swDrainPump.onToggleFn = {
            ctrl(3, it)
        }
        swV1.onToggleFn = {
            ctrl(4, it)
        }
        swV2.onToggleFn = {
            ctrl(5, it)
        }
        swV3.onToggleFn = {
            ctrl(6, it)
        }
        swV4.onToggleFn = {
            ctrl(7, it)
        }
        swV5.onToggleFn = {
            ctrl(8, it)
        }
        waterHeat.onToggleFn = {
            ctrl(12, it)
        }
        steamHeat.onToggleFn = {
            ctrl(13, it)
        }
        drawPump.onToggleFn = {
            ctrl(14, it)
        }
    }

    private suspend fun ctrl(id: Int, v: Boolean) {
        try {
            val value = if (v) 0 else 1
            partsCtrl(id, value)
        } catch (e: Exception) {
            showErr("错误", e)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    fun onStatusUpdate(status: DeviceStatus) {
        val ctrl = status.ctrlStatus
        swLed.set(ctrl.roomLed)
        swRoomFan.set(ctrl.roomFan)
        swDrainFan.set(ctrl.drainFan)
        swDrainPump.set(ctrl.drainPump)
        swV1.set(ctrl.v1)
        swV2.set(ctrl.v2)
        swV3.set(ctrl.v3)
        swV4.set(ctrl.v4)
        swV5.set(ctrl.v5)
        waterHeat.set(ctrl.heatWater)
        steamHeat.set(ctrl.heatSteam)
        drawPump.set(ctrl.waterPump)
    }

    override fun onWindowVisibilityChanged(visibility: Int) {
        super.onWindowVisibilityChanged(visibility)
        if (visibility == VISIBLE) {
            register(this)
        } else {
            unregister(this)
        }
    }
}